Skip to content

perf: reuse zend max execution timers between requests - #25673

Open
henderkes wants to merge 6 commits into
php:masterfrom
henderkes:perf/zts-timer-reuse
Open

henderkes wants to merge 6 commits into
php:masterfrom
henderkes:perf/zts-timer-reuse

Conversation

@henderkes

@henderkes henderkes commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

logic on master: each request creates a timer, sets time limit, deletes the timer
logic here: each thread creates a timer, each request sets time limit, then after the request sets time limit to zero. timer is reused by the next request.

this skips noticeable overhead, especially in threaded scenarios (first measurement in frankenphp ~4% for hello world), but also in NTS (if max execution timers are enabled)

FreeBSD excluded for now because of https://github.com/freebsd/freebsd-src/blob/main/sys/sys/timers.h#L65

cc @dunglas

remember it once for all threads, so a host signal still reaches its handler if it lands on a different PHP thread
@henderkes

henderkes commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor Author

4 core workload:

Stack Workload Base req/s PR req/s Paired change [95% interval]
NTS FPM + nginx Hello 40,860 41,292 +1.06% [+0.86, +1.25]
NTS FPM + nginx Symfony 1,495 1,502 +0.49% [+0.23, +0.74]
ZTS FPM + nginx Hello 39,863 40,317 +1.14% [+0.91, +1.37]
ZTS FPM + nginx Symfony 1,451 1,458 +0.50% [+0.26, +0.75]
Classic FrankenPHP Hello 40,607 41,601 +2.45% [+1.61, +3.29]
Classic FrankenPHP Symfony 1,329 1,340 +0.85% [+0.61, +1.10]

Benchmarked and table generated by gpt, but I got very similar numbers in manual wrk runs, so I think it's more or less correct in the realm of local hardware not being the most stable.

WIll measure gains with higher concurrency later Edit: will not, big.LITTLE architecture on my hardware seems to lead to very inconsistent results.

cc @Girgias

@henderkes
henderkes marked this pull request as ready for review November 24, 2026 04:49

@iliaal iliaal left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix the NTS timeout regression before merging. I’d also like deployments with a tight pending-signal limit to be able to keep execution timers enabled without retaining them between requests.

Comment thread Zend/zend_max_execution_timer.c Resolved
static const struct itimerspec disarmed = {0};
zend_cached_timer *cache = TIMER_CACHE;
if (cache && cache->pid == EG(pid) && CAN_CACHE_TIMER()
&& timer_settime(EG(max_execution_timer_timer), 0, &disarmed, NULL) == 0) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A disarmed timer still consumes one RLIMIT_SIGPENDING slot for the real UID. With the limit set to 1, two php -S processes under the same UID can serve one request each, sequentially, on master. Here the first server keeps the slot while idle, and the second fails at request startup with EAGAIN.

--FILE--
<?php
include "php_cli_server.inc";
php_cli_server_start(<<<'PHP'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please set PHP_CLI_SERVER_WORKERS= in --ENV--, as serialization_context_request_reset.phpt does. With four workers, this test passed with both busy requests on one PID and the intervening idle request on another.

@henderkes

Copy link
Copy Markdown
Contributor Author

I’d also like deployments with a tight pending-signal limit to be able to keep execution timers enabled without retaining them between requests.

Isn't the limit ridiculously high on Linux? That's the reason it's only enabled there.

I could check limits on init once before but what's really a sane number where we say reuse is ok? If we check it per request against the currently active number of threads, I would imagine it's going to eat the savings of reusing them.

@iliaal

iliaal commented Nov 24, 2026

Copy link
Copy Markdown
Member

The limit is high here too, and other processes under the same real UID share it. I’d keep reuse on by default and let admins disable just reuse at startup. That avoids both a threshold and a per-request check.

@henderkes

Copy link
Copy Markdown
Contributor Author

So I figured out why it's so high on linux and it's because it uses RLIMIT_NPROC, i.e. how many processes can exist at a time. Since fpm doesn't check the rlimit for whether it can fork X children, I don't think it's worth an extra setting here either.

What I still want to do here though is maybe not disarm and already disarmed timer.

The other two comments look sensible and I'll change it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants